let fs = require('fs');
module.exports = {
  manage: async function (req, res) {
    res.view({layout: 'layouts/manage_layout',nc:req.session.adminZh.nc})
  },
  login: function (req, res) {
    res.view({layout: false})
  },
  use: function (req, res) {
    res.view({layout: 'layouts/manage_layout'})
  },
  manage_yzm: function (req, res) {
    let captcha = require('svg-captcha').create();
    req.session.captcha_admin = captcha.text;
    res.send(captcha.data);
  },
  manage_login: async function (req, res) {
    let params = req.allParams();
    if(req.session.captcha_admin!=params.yzm){
      return res.json(3)
    }
    delete params.yzm;
    let row=await Manage.findOne(params);
    if(row==undefined)return res.json(2);
    req.session.adminZh={zh:row.zh,nc:row.nc};
    return res.json(1)
  },
  register: async function (req, res) {
    let params = req.allParams();
    if (params.pwd != params.repwd) {
      return res.json(3);
    }
    let row=await Manage.findOne({zh:params.zh});
    if(row!=undefined)return res.json(2);
    row = await Manage.create(params).fetch();
    req.session.adminZh = {zh: row.zh, nc: row.nc};
    return res.json(1);
  },
  logout: function (req, res) {
    delete req.session.adminZh;
    res.redirect('/manage/login')
  },
  edit: async function (req, res) {
    res.view({layout: 'layouts/manage_layout'})
  },
  insert: function (req, res) {
    req.file('img').upload(
      {
        dirname: require('path').resolve(sails.config.appPath, 'assets/uplods')
      },
      async function (err, files) {
        if (err)
          return res.serverError(err);
        let ext = files[0].filename.split('.');
        console.log(files[0].filename)
        ext = ext[ext.length - 1];
        if (['jpg', 'gif', 'png'].indexOf(ext) == -1) {
          return res.json(2);
        }

        let path = files[0].fd.split('\\');
        path = path[path.length - 1];
        let parames = req.allParams();
        parames.img = path;
        let row = await shop.create(parames).fetch();
        return res.json(1);
      });
  },
  get_page: async function (req, res) {
    let arr = await shop.find({})
    res.json({arr:arr});
  },
  get_user: async function (req, res) {
    let arr = await User.find({})
    console.log(arr)
    res.json({arr:arr});
  },
  delete: async function (req, res) {
    let arr = await shop.destroy({id: req.query['id']}).fetch();
    fs.unlink('assets/uploads/' + req.query['img'], function () {
      res.json(1);
    });
  },
  dele: async function (req, res) {
    let arr = await User.destroy({id: req.query['id']}).fetch();
      res.json(1);
  },
  get_one: async function (req, res) {
    let arr = await shop.findOne({id: req.query['id']});
    res.json(arr);
  },
  get_onee: async function (req, res) {
    let arr = await User.findOne({id: req.query['id']});
    res.json(arr);
  },
  change: function (req, res) {
    req.file('new_src').upload(
      {
        dirname: require('path').resolve(sails.config.appPath, 'assets/uplods')
      },
      async function (err, files) {
        if (err)
          return res.serverError(err);
        if (files.length === 0) {
          let params = req.allParams();
          let rs = await shop.update({id: params.id}, params).fetch();
          if (rs.length > 0) {
            return res.json(1)
          } else {
            return res.json(3)
          }
        }
        let ext = files[0].filename.split('.');
        ext = ext[ext.length - 1];
        if (['jpg', 'gif', 'png'].indexOf(ext) == -1) {
          return res.json(2);
        }
        let path = files[0].fd.split('\\');
        path = path[path.length - 1];
        let parames = req.allParams();
        parames.img = path;
        fs.unlinkSync('assets/uplods/' + parames.old_src);
        let rs = await shop.update({id: parames.id}, parames).fetch();
        if (rs.length > 0) {
          return res.json(1)
        } else {
          return res.json(3)
        }
      });
  },
  gaiy: async function (req, res) {
    let parames = req.allParams();
    let rs = await User.update({id: parames.id}, parames).fetch()
    res.json(1);
  },
};

